package reduce.fz.com.reduce.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.constant.ParamConst;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * Team小同伴动态list
 */
public class TeamDynamicListFragment extends BaseListFragment<TeamDynamicListFragment.TeamDynamicViewHolder, TeamDynamic> {

    private Long teamid;

    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();


    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, recyclerView);
    }

    public static TeamDynamicListFragment create(Long teamid) {
        TeamDynamicListFragment fragment = new TeamDynamicListFragment();
        fragment.setSupportLoadingDialog(true);
        Bundle bundle = new Bundle();
        bundle.putLong("teamid", teamid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEvent(PubDynamicEvent event) {
        TeamDynamic teamDynamic = event.dynamic;
        addFrist(teamDynamic);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        teamid = getArguments().getLong("teamid");

        return view;

    }

    public TeamDynamicListFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表方式
        return null;
    }

    @Override
    protected TeamDynamicViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_team_my_dynamic_card, parent, false);
        TeamDynamicViewHolder holder = new TeamDynamicViewHolder(view);
        return holder;
    }

    @Override
    protected TeamDynamicViewHolder createfootViewHolder(View view) {
        return new TeamDynamicViewHolder(view);
    }


    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<TeamDynamic>>(new TypeReference<Result<List<TeamDynamic>>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<List<TeamDynamic>> data) {
                addResult(data);
            }
        });
    }


    @Override
    protected String setUrlBeforeRequest(List<TeamDynamic> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_team_dynamic_list, teamid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }


    public static class TeamDynamicViewHolder extends BaseRecycleViewHolder<TeamDynamic> {


        private RoundNetworkImageView mSrcUserAvatar;
        private TextView mSrcNickname;
        private TextView mSrcDesc;
        private LinearLayout mllSrc;
        private TextView mPubtime;


        public TeamDynamicViewHolder(View itemView) {
            super(itemView);
            mSrcDesc = (TextView) itemView.findViewById(R.id.tv_src_user_desc);
            mSrcUserAvatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_src_avatar);
            mSrcNickname = (TextView) itemView.findViewById(R.id.tv_src_user_nickname);
            mllSrc = (LinearLayout) itemView.findViewById(R.id.ll_src_user);
            mPubtime=(TextView)itemView.findViewById(R.id.tv_pub_time);

        }

        @Override
        public void renderView(TeamDynamic data, int position) {
            if (data.dynamictype == ParamConst.TEAM_DYNAMIC_TYPE_PUB_WITNESS) {
                renderShortDesc(data);
            } else {
                renderShortDesc(data);
            }
        }

        /**
         * join动态
         *
         * @param data
         */


        public void renderShortDesc(TeamDynamic data) {
            mllSrc.setVisibility(View.VISIBLE);
            StringBuffer sr = new StringBuffer("");
            sr.append(data.getShortDesc());
            mSrcDesc.setText(sr.toString());
            mSrcUserAvatar.setImageUrl(data.srcuser.avatar, HttpUtils.getAvatarImageLoader());
            mSrcNickname.setText(data.srcuser.nickname);
            mPubtime.setText(DateUtil.getInterval(data.pubtime));
        }






    }


}
